#
# This is a source file that is used by a 'require name.pl'
# to load into the global namespace.
#
# It is expected to set the following variable:
#
%::PROVIDES=(
	name	=> "RECONFIGURE",
	desc	=> "Rereconfigure running cluster daemon",
	type	=> "request",
	code	=> \&::do_cldaemon_reconfigure
);

sub ::do_cldaemon_reconfigure {
my $args=shift;
my ($nxml,$err,@errmsg);
my $changes=0;
my $warnings=0;

	#################################################################
	# Check the config file is valid xml and go not futher if not.	#
	#################################################################

	($nxml,$err,@errmsg)=validate_cluster_cfg($::ARG_config,
		$::ARG_nochecksums);

	if(!defined($nxml)) {
		return "FAIL ".join("|",@errmsg);
	}

	# The following globals need to be checked and compared
	# to the new values. Certain things are ignored, and if
	# so a suitable warning in each case should be logged 
	# to the log file.
	# The list below gives variable, and action.

	# variable       XML                   ACTION
	# server_port	 global->port          Ignore, give warning
	# server_key	 global->key           Ignore, give warning
	# logdir	 global->logdir        Ignore, give warning
	# time_keepalive timings->keepalive    Change, give notice
	# time_warn      timings->warn         Change, give notice
	# time_dead      timings->dead         Change, give notice
	# CLUSTER_NAME   global->name          Change, give notice
	#                global->drbd_network  Ignore, give warning

	#################################################################
	# Indicate the params that can not currently be changed.	#
	#################################################################

	if($nxml->{global}->{key} ne $::server_key) {
		logwarn("Cluster key changed - not dynamic - ignored.");
		$warnings++;
	}
	if($nxml->{global}->{logdir} ne $::logdir) {
		logwarn("Cluster log directory changed - not dynamic - ignored.");
		$warnings++;
	}
	if($nxml->{global}->{drbd_network} ne $::CLXML->{global}->{drbd_network}) {
		logwarn("Cluster DRBD network changed - not dynamic - ignored.");
		$warnings++;
	}
	
	#################################################################
	# Any simple parameter changes take affect now...		#
	#################################################################

	if(exists($nxml->{timings}->{keepalive}) &&
		$nxml->{timings}->{keepalive} != $::time_keepalive) {
		$::time_keepalive=$nxml->{timings}->{keepalive};
		logmsg("UPDATED: timings.keepalive = $::time_keepalive");
		$changes++;
	}
	if(exists($nxml->{timings}->{warn}) &&
		$nxml->{timings}->{warn} != $::time_warn) {
		$::time_warn=$nxml->{timings}->{warn};
		logmsg("UPDATED: timings.warn = $::time_warn");
		$changes++;
	}
	if(exists($nxml->{timings}->{dead}) &&
		$nxml->{timings}->{dead} != $::time_dead) {
		$::time_dead=$nxml->{timings}->{dead};
		logmsg("UPDATED: timings.dead = $::time_dead");
		$changes++;
	}

	#################################################################
	# A check to ensure the nodes have not changed makes sense.	#
	#################################################################

	my @nodes=keys(%{$nxml->{node}});
	if(!exists($::CLXML->{node}->{$nodes[0]}) ||
		!exists($::CLXML->{node}->{$nodes[1]})) {
		logwarn("Cluster defines different node set - ignoring.");
		$warnings++;
	}

	#################################################################
	# At this point check for any running applications on this	#
	# node and then request a lems reconfigure request in each	#
	# instance.							#
	#################################################################
	my ($capp);
	for $capp (sort keys(%::appmap)) {
		if($::appmap->{$capp}->{STATE} ne "STARTED") {
			next;
		}
		if($::appmap->{$capp}->{NODE} ne hostname) {
			next;
		}
		check_n_restart_lems($capp,"RECONFIGURE");
	}

	#################################################################
	# Finally issue a message summarising any changes.		#
	#################################################################

	logmsg("UPDATE COMPLETE (OK): $warnings Warnings, $changes Changes.");
	return "OK";
}

1;

